<?php

class challenge extends CI_Controller {

	private $date_format;

	/**
	 * Default Constructor
	 */
	function __construct(){
		parent::__construct();
		$this->date_format = '%Y-%m-%e';
	}

	/**
	 * This function checks the provided date to make sure that it is in the future
	 * */
	function check_date($str){
		$this->load->helper('date');
		$date = strptime($str, $this->date_format);
		if ($date == FALSE){
			$this->form_validation->set_message('check_date', 'Invalid %s Entered');
			return FALSE;
		} else if (strtotime($str) < now()){
			$this->form_validation->set_message('check_date', 'Date has to be in the future');
			return FAlSE;
		} else {
			return TRUE;
		}
	}
	
	
	/**accepts a pending challenge.
	 * this changes it sstatus in the database 
	 * and then redirects to the profile page*/
	function accept_challenge(){
		$id = $this->input->post('set_id');
		$accept = $this->input->post('submit');
		
		if(!empty($id)){
			$challenge = new challenge_object();
			$challenge = $challenge->load_challenge($id);
			$challenge->accept_challenge($accept == 'Accept' ? true : false);
		}
		
		redirect('/profile/show/' . $this->session->userdata('account'));
	}
	
	/**
	 * This method reports the results from a game, uploading the results in othe database
	 * */
	function report_game(){
		$input_data = array (
	        //'player1' => $this->input->post('player1'),
	        //'player2' => $this* ->input->post('player2'),
			'map' => $this->input->post('map'),
			'winner' => $this->input->post('winner'),
			'replay' => $this->input->post('replay'),
			'game_set_id' => $this->input->post('set_id')
		);
		
		if(!empty($input_data['game_set_id'])){
			$challenge = new challenge_object();
			$challenge = $challenge->load_challenge($input_data['game_set_id']);
			$error = $challenge->report_game($input_data);
		}
		
		if (isset($error)){
			echo $error;
		}
		redirect('/profile/show/' . $this->session->userdata('account'));
	}
}